//
//  UIView+Gradient.m
//  SHShopping
//
//  Created by 坤鼎 on 2019/9/2.
//  Copyright © 2019 kunding. All rights reserved.
//

#import "UIView+Gradient.h"

@implementation UIView (Gradient)

/**
 *  根据给定的颜色，设置按钮的颜色
 *  @param colorArray     渐变颜色的数组
 *  @param pointArr       startPoint & endPoint
 */
- (UIView *)gradientViewColorArray:(NSArray *)colorArray pointArr:(NSArray *)pointArr {
    
    CAGradientLayer *gradientLayer =  [CAGradientLayer layer];
    gradientLayer.backgroundColor = [UIColor clearColor].CGColor;
    gradientLayer.frame = self.bounds;
    if (IsNotNilArr(pointArr)) {
        gradientLayer.startPoint = CGPointMake([pointArr[0] doubleValue],
                                               [pointArr[1] doubleValue]);
        gradientLayer.endPoint = CGPointMake([pointArr[2] doubleValue],
                                             [pointArr[3] doubleValue]);
    } else {
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
    }
    if (IsNotNilArr(colorArray)) {
        [gradientLayer setColors:colorArray];//渐变数组
    } else {
        [gradientLayer setColors:@[(id)[UIColorFromRGB(0x6DE4FF) CGColor],
                                   (id)[UIColorFromRGB(0x3170FF) CGColor]]];//渐变数组
    }
    gradientLayer.locations = @[@(0),
                                @(1)];//渐变点
    
    [self.layer addSublayer:gradientLayer];
    self.layer.cornerRadius = self.frame.size.height / 2;
    self.layer.masksToBounds = YES;
    return self;
}

@end
